﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZHDJ.Core;

namespace ZHDJ.Data.Models.Common
{
    /// <summary>
    /// 评论表
    /// </summary>
    public class PartyComment : EntityBase
    {
        /// <summary>
        /// 关联ID
        /// </summary>
        public string RelationID { get; set; }

        /// <summary>
        /// 评论内容
        /// </summary>
        public string Content { get; set; }

        /// <summary>
        /// 评论图片
        /// </summary>
        public string ImageID { get; set; }

        /// <summary>
        /// 评论图片描述
        /// </summary>
        public string ImageDesc { get; set; }

        /// <summary>
        /// 所属模块
        /// </summary>
        public string Module { get; set; }

        /// <summary>
        /// （保留）数据创建方式 （网站，手机端。。）
        /// </summary>
        public string DataCreateType { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        public string CreateBy { get; set; }

        public static DataSet GetCommentData(int pageIndex, int pageSize, string relationId)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"select SQL_CALC_FOUND_ROWS UserName,Content,`comment`.CreateTime from tblpartycomment `comment`
left join tblpartyorgmember member on `comment`.CreateBy=LoginID where RelationID=?RelationID");

            sb.Append(" order by `comment`.CreateTime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");

            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter()
                {
                    DbType = DbType.String,
                    ParameterName = "RelationID",
                    Value = relationId
                },
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "Begin",
                    Value = (pageIndex)*pageSize
                },
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "PageSize",
                    Value = pageSize
                }
            };

            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }

        public static DataSet GetCommentData(string relationId)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"select UserName,Content,`comment`.CreateTime,DataCreateType,ImageID,ImageDesc from tblpartycomment `comment`
left join tblpartyorgmember member on `comment`.CreateBy=LoginID where RelationID=?RelationID");

            sb.Append(" order by `comment`.CreateTime desc");

            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter()
                {
                    DbType = DbType.String,
                    ParameterName = "RelationID",
                    Value = relationId
                }
            };

            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }
    }
}
